﻿using System;
using System.IO;
using BorgExt;

namespace Borg.Desktop
{
    internal class FileLogger
    {
        private readonly StreamWriter _writer;

        internal FileLogger(string logfile, bool noLogo)
        {
            _writer = new StreamWriter(logfile);

            if (!noLogo)
                _writer.WriteLine(BorgExt.Runtime.VersionInfo);

            _writer.Flush();
        }

        internal void OnLog(object sender, BorgExt.LogEventArgs args)
        {
            if (args.Type != LogType.Info)
            {
                _writer.WriteLine(
                    args.Type == LogType.Error
                    ? "\n--ERROR:"
                    : "\n--WARNING:");

                _writer.WriteLine(args.Message);
                _writer.WriteLine();
            }
            else
                _writer.WriteLine(args.Message);

            _writer.Flush();
        }

        internal void OnShutdownFinished(object sender, EventArgs e)
        {
            _writer.Close();
        }
    }
}
